package net.realsee.controller.wcs.intf;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import net.realsee.common.annotation.Log;
import net.realsee.common.annotation.RepeatSubmit;
import net.realsee.common.enums.BusinessType;
import net.realsee.common.enums.wcs.intf.*;
import net.realsee.common.utils.poi.ExcelUtil;
import net.realsee.domain.wcs.intf.bo.WcsIntfBo;
import net.realsee.domain.wcs.intf.vo.WcsIntfVo;
import net.realsee.framework.core.controller.BaseController;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.domain.R;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.validate.AddGroup;
import net.realsee.framework.core.validate.EditGroup;
import net.realsee.service.wcs.intf.IWcsIntfService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 接口管理
 *
 * @author realsee
 * @date 2023-08-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wcs/intf")
public class WcsIntfController extends BaseController {

    private final IWcsIntfService iWcsIntfService;

    /**
     * 查询接口管理列表
     */
    @SaCheckPermission("wcs:intf:list")
    @GetMapping("/list")
    public TableDataInfo<WcsIntfVo> list(WcsIntfBo bo, PageQuery pageQuery) {
        return iWcsIntfService.selectVoPage(bo, pageQuery);
    }

    /**
     * 导出接口管理列表
     */
    @SaCheckPermission("wcs:intf:export")
    @Log(title = "接口管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WcsIntfBo bo, HttpServletResponse response) {
        List<WcsIntfVo> list = iWcsIntfService.selectVoList(bo);
        ExcelUtil.exportExcel(list, "接口管理", WcsIntfVo.class, response);
    }

    /**
     * 获取接口管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wcs:intf:query")
    @GetMapping("/{id}")
    public R<WcsIntfVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long id) {
        return R.ok(iWcsIntfService.selectVoById(id));
    }

    /**
     * 新增接口管理
     */
    @SaCheckPermission("wcs:intf:add")
    @Log(title = "接口管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WcsIntfBo bo) {
        return toAjax(iWcsIntfService.insertByBo(bo));
    }

    /**
     * 修改接口管理
     */
    @SaCheckPermission("wcs:intf:edit")
    @Log(title = "接口管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WcsIntfBo bo) {
        return toAjax(iWcsIntfService.updateByBo(bo));
    }

    /**
     * 删除接口管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wcs:intf:remove")
    @Log(title = "接口管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iWcsIntfService.deleteWithValidByIds(Arrays.asList(ids)));
    }

    /**
     * 查询接口类别列表
     */
    @GetMapping("/listInterfaceClass")
    public R<List<String>> listInterfaceClass() {
        return R.ok(Arrays.stream(InterfaceClassEnum.values()).map(InterfaceClassEnum::getCode).collect(Collectors.toList()));
    }

    /**
     * 查询接口Code列表
     */
    @GetMapping("/listInterfaceCode")
    public R<List<String>> listInterfaceCode() {
//        // 1、系统定义的接口
//        List<String> codeListInEnum = Arrays.stream(InterfaceCodeEnum.values()).map(InterfaceCodeEnum::getCode).collect(Collectors.toList());
//        // 2、已设置的接口
//        List<String> codeListInDB = iWcsIntfService.listInterfaceCodeInDB();
//        return R.ok(codeListInEnum.stream().filter(item -> !codeListInDB.contains(item)).collect(Collectors.toList()));
        return R.ok(Arrays.stream(InterfaceCodeEnum.values()).map(InterfaceCodeEnum::getCode).collect(Collectors.toList()));
    }

    /**
     * 查询接口方法列表
     */
    @GetMapping("/listInterfaceMethod")
    public R<List<String>> listInterfaceMethod() {
        return R.ok(Arrays.stream(InterfaceMethodEnum.values()).map(InterfaceMethodEnum::name).collect(Collectors.toList()));
    }

    /**
     * 查询接口类别列表
     */
    @GetMapping("/listInterfaceType")
    public R<List<String>> listInterfaceType() {
        return R.ok(Arrays.stream(InterfaceTypeEnum.values()).map(InterfaceTypeEnum::name).collect(Collectors.toList()));
    }

    /**
     * 查询接口认证方式列表
     */
    @GetMapping("/listInterfaceAuthType")
    public R<List<String>> listInterfaceAuthType() {
        return R.ok(Arrays.stream(InterfaceAuthTypeEnum.values()).map(InterfaceAuthTypeEnum::name).collect(Collectors.toList()));
    }
}
